package cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods.vo.GroupGoodsExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods.vo.GroupGoodsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods.vo.GroupGoodsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.dal.dataobject.groupgoods.GroupGoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.dal.mysql.groupgoods.GroupGoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.service.groupgoods.GroupGoodsService;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.dal.mysql.group.GroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 拼团商品")
@RestController
@RequestMapping("/qd/group-goods")
@Validated
public class GroupGoodsController {

    @Resource
    private GroupGoodsService groupGoodsService;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    GroupMapper groupMapper;
    @Resource
    GroupGoodsMapper groupGoodsMapper;

    //模糊查询商品名称
    @GetMapping("updateStatus")
    public CommonResult updateStatus(Integer id,Integer type){
        GroupGoodsDO dto = groupGoodsMapper.selectById(id);
        if (dto==null)
            throw new ServiceException(500,"活动不存在");

        dto.setStatus(type);
        groupGoodsMapper.updateById(dto);
        return success("");
    }

    //模糊查询商品名称
    @GetMapping("findByName")
    public CommonResult findByName(String name){
        List<GoodsDO> list=new ArrayList<>();
        if (name!=null){
            list=goodsMapper.selectList(new LambdaQueryWrapper<GoodsDO>().like(GoodsDO::getName,name));
        }

        return success(list);
    }

    //查询拼团模块
    @GetMapping("findGroup")
    public CommonResult findGroup(){
        return success(groupMapper.selectList());
    }

    @PostMapping("/create")
    @Operation(summary = "创建拼团商品")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:create')")
    public CommonResult<Integer> createGroupGoods(@Valid @RequestBody GroupGoodsDO createReqVO) {
        return success(groupGoodsService.createGroupGoods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拼团商品")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:update')")
    public CommonResult<Boolean> updateGroupGoods(@Valid @RequestBody GroupGoodsDO updateReqVO) {
        groupGoodsService.updateGroupGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拼团商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:group-goods:delete')")
    public CommonResult<Boolean> deleteGroupGoods(@RequestParam("id") Integer id) {
        groupGoodsService.deleteGroupGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拼团商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:query')")
    public CommonResult<GroupGoodsDO> getGroupGoods(@RequestParam("id") Integer id) {
        return success(groupGoodsService.getGroupGoods(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得拼团商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:query')")
    public CommonResult getGroupGoodsList(@RequestParam("ids") Collection<Integer> ids) {
        return success(groupGoodsService.getGroupGoodsList(ids));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拼团商品分页")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:query')")
    public CommonResult getGroupGoodsPage(@Valid GroupGoodsPageReqVO pageVO) {
        return success(groupGoodsService.getGroupGoodsPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拼团商品 Excel")
    @PreAuthorize("@ss.hasPermission('qd:group-goods:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupGoodsExcel(@Valid GroupGoodsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
//        List<GroupGoodsDO> list = groupGoodsService.getGroupGoodsList(exportReqVO);
//        // 导出 Excel
//        List<GroupGoodsExcelVO> datas = GroupGoodsConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "拼团商品.xls", "数据", GroupGoodsExcelVO.class, datas);
    }

}
